package vtt.android.docbaovn.database;

import vtt.android.library.utils.VttLog;
import vtt.android.docbaovn.BaseApplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

	private static final String TAG = "MySQLiteOpenHelper";
	// Database Version
	private static final int DATABASE_VERSION = 1;
	// Database Name
	private static final String DATABASE_NAME = "vtt_docbao.db";
	private Context mContext;
	VttLog log = new VttLog(TAG, BaseApplication.isDebuggable);

	public MySQLiteOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.mContext = context;
	}

	// Creating Tables
	@Override
	public void onCreate(SQLiteDatabase db) {
		createPageTable(db);
		createCategoryTable(db);
		createRssSourceTable(db);
	}

	// Upgrading database
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Drop older table if existed

		db.execSQL("DROP TABLE IF EXISTS " + "page");
		// Create tables again
		onCreate(db);
	}

	private void createPageTable(SQLiteDatabase db) {
		String sql = " CREATE TABLE page ( " 
				+ " code   TEXT, " 
				+ " name_full TEXT " 
				+ ");";

		db.execSQL(sql);

		String query = "INSERT INTO [page] ([code], [name_full]) VALUES ";
		db.execSQL(query + "('VnExpress', 'VnExpress');");
		db.execSQL(query + "('DanTri', 'Dan Tri');");
		db.execSQL(query + "('24h', '24h.com.vn');");
		db.execSQL(query + "('DoiSongPhapLuat', 'www.doisongphapluat.com');");
		db.execSQL(query + "('dantri', 'dantri.com.vn');");
		db.execSQL(query + "('haivl', 'haivl.com');");
		db.execSQL(query + "('pcworld', 'pcworld.com.vn');");
		db.execSQL(query + "('vneconomy', 'vneconomy');");
		
	}
	
	/**
	 * create table chuyen muc
	 * @param db
	 */
	private void createCategoryTable(SQLiteDatabase db) {
		String sql = " CREATE TABLE category ( " 
				+ " type TEXT, " 
				+ " name TEXT, "
				+ " active TEXT( 2 )  DEFAULT ( 1 ) "
				+ ");";

		db.execSQL(sql);
		
		String query = "INSERT INTO [category] ([type], [name], [active]) VALUES ";
		db.execSQL(query + "('new', 'Tin nổi bật', '1');");
		db.execSQL(query + "('world', 'Thế giới', '1');");
		db.execSQL(query + "('sport', 'Thể Thao', '1');");
		db.execSQL(query + "('technology', 'Công nghệ', '1');");
		db.execSQL(query + "('business', 'Kinh doanh', '1');");
		db.execSQL(query + "('law', 'Pháp Luật', '1');");
		db.execSQL(query + "('fun', 'Cười', '1');");
	}

	private void createRssSourceTable(SQLiteDatabase db) {
		String sql = " CREATE TABLE rss_source ( " 
				+ " id INTEGER PRIMARY KEY ASC AUTOINCREMENT, "
				+ " page_code TEXT, " 
				+ " category_type TEXT, " 
				+ " link TEXT, " 
				+ " active TEXT( 2 )  DEFAULT ( 1 )"
				+ ");";
		
		db.execSQL(sql);
		
		String query = "INSERT INTO [rss_source] ([page_code], [category_type], [link], [active]) VALUES ";
		// new
		db.execSQL(query + "('vnexpress', 'new', 'http://vnexpress.net/rss/tin-moi-nhat.rss', '1');");
		db.execSQL(query + "('24h', 'new', 'http://www.24h.com.vn/upload/rss/tintuctrongngay.rss', '1');");
		db.execSQL(query + "('dantri', 'new', 'http://dantri.com.vn/trangchu.rss', '1');");
		// world 
		db.execSQL(query + "('vnexpress', 'world', 'http://vnexpress.net/rss/the-gioi.rss', '1');");
		// sport
		db.execSQL(query + "('vnexpress', 'sport', 'http://thethao.vnexpress.net/rss/tin-moi-nhat.rss', '1');");
		db.execSQL(query + "('24h', 'sport', 'http://www.24h.com.vn/upload/rss/bongda.rss', '1');");
		// business
		db.execSQL(query + "('vnexpress', 'business', 'http://kinhdoanh.vnexpress.net/rss/tin-moi-nhat.rss', '1');");
		db.execSQL(query + "('vneconomy', 'business', 'http://vneconomy.vn/rss/trang-chu', '1');");
		
		// Law
		db.execSQL(query + "('vnexpress', 'law', 'http://vnexpress.net/rss/phap-luat.rss', '1');");
		db.execSQL(query + "('DoiSongPhapLuat', 'law', 'http://www.doisongphapluat.com/rss/phap-luat.rss', '1');");
		// Technology
		db.execSQL(query + "('vnexpress', 'technology', 'http://sohoa.vnexpress.net/rss/san-pham/dien-thoai', '1');");
		db.execSQL(query + "('vnexpress', 'technology', 'http://sohoa.vnexpress.net/rss/san-pham/may-tinh-bang', '1');");
		db.execSQL(query + "('vnexpress', 'technology', 'http://sohoa.vnexpress.net/rss/san-pham/laptop', '1');");
//		db.execSQL(query + "('pcworld', 'technology', 'http://rss.pcworld.com.vn/articles/cong-nghe.rss', '1');");
		
		// Fun
		db.execSQL(query + "('haivl', 'fun', 'http://www.haivl.com/rss', '0');");
		db.execSQL(query + "('24h', 'fun', 'http://www.24h.com.vn/upload/rss/cuoi24h.rss', '1');");
	}
}
